package com.kamistoat.meimeimall.meimeimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.meimeimallproduct.entity.ProductAttrValueEntity;
import com.kamistoat.meimeimall.meimeimallproduct.service.ProductAttrValueService;
import com.kamistoat.meimeimall.meimeimallproduct.service.SpuInfoService;
import com.kamistoat.meimeimall.meimeimallproduct.vo.AttrResponseVo;
import com.kamistoat.meimeimall.meimeimallproduct.vo.AttrVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeimall.meimeimallproduct.service.AttrService;
import com.kamistoat.meimeimall.common.utils.R;


/**
 * 商品属性
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 15:16:42
 */
@Api("商品属性数据库操作")
@RestController
@RequestMapping("attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 查询对应三级菜单属性
     */
    @ApiOperation("查询指定商品目录的全部属性")
    @GetMapping("/catelog_attr_list/{catelogId}/{attrType}")
    @DubboAndHTTPExceptionCatch
    public R baseAttrList(@PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type,
                          @RequestParam Map<String, Object> params) {
        return R.ok();
    }

    /**
     * 返回当前SPU的属性值
     *
     * @param spuId
     * @return
     */
    @ApiOperation("查询指定SpuId的全部属性")
    @GetMapping("/spu_attr_list/{spuId}")
    @DubboAndHTTPExceptionCatch
    public R listForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.listForSpu(spuId);
        return R.ok().put("data", productAttrValueEntityList);
    }


    /**
     * 修改了spu的基础属性后，对数据库进行更新，同时对ES中的数据也进行更新
     *
     * @param spuId                      spuId
     * @param productAttrValueEntityList 基础属性值列表
     * @return
     */
    @ApiOperation("修改指定SpuId的属性")
    @PostMapping("/spu_attr_list/{spuId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R updateBySpuId(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> productAttrValueEntityList) {
        productAttrValueService.updateBySpuId(spuId, productAttrValueEntityList);
        // 对ES的数据进行更新，直接重新提交新数据就行，会覆盖掉同一个ID的原始数据
        spuInfoService.spuUp(spuId);
        return R.ok();
    }

    /**
     * 信息
     */
    @ApiOperation("查询指定attrId的属性详情")
    @GetMapping("/info/{attrId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        return R.ok();
    }

    /**
     * 保存
     */
    @ApiOperation("向数据库保存POJO")
    @PutMapping("/attr_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attr:save")
    public R save(@RequestBody AttrVo attrVo) {

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("向数据库更新POJO")
    @PostMapping("/attr_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attr:update")
    public R update(@RequestBody AttrVo attrVo) {
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("从数据库删除POJO")
    @DeleteMapping("/attr_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
